package com.example.lecturereservation.service;

import com.example.lecturereservation.entity.Users;
import com.example.lecturereservation.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UsersService implements UsersMapper{

    @Autowired
    UsersMapper usersMapper;

    //注册功能
    public String registration(Users users){

        if (users.getEmail().trim().length()==0){
            return "邮箱不能为空";
        }

        if (users.getEmail().indexOf("@")==-1){
            return "邮箱格式不对";
        }

        //查找邮箱存在
        if (usersMapper.findByEmail(users.getEmail())!=null){
            return "用户已经存在！";
        }

        if (users.getPassword().trim().length()<6){
            return "密码长度必须大于6";
        }

        this.save(users);
        return "ok";
    }


    @Override
    @Cacheable//缓存：只取1次，第二次从缓存中取数据
    public Users getById(int id) {
        return usersMapper.getById(id);
    }

    @Override
    public void save(Users t) {
        this.usersMapper.save(t);
    }

    @Override
    public Users findByEmail(String email) {
        return this.usersMapper.findByEmail(email);
    }

    @Override
    public void deleteById(int id) {

    }

    @Override
    public void update(Users t) {

    }

    @Override
    public List<Users> getAll() {
        return null;
    }

    @Override
    public List<Users> getPage(int start, int end) {
        return null;
    }
}
